<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <title>后台管理系统</title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css" rel="stylesheet">
    <link href="/css/login.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html"/>
    <![endif]-->
    <script>
        if (window.top !== window.self) {
            window.top.location = window.location;
        }
    </script>

</head>

<body class="signin">
<div class="signinpanel">
    <div class="row">
        <div class="col-sm-7">
            <div class="signin-info">
                <div class="logopanel m-b">
                    <h1>水立方后台管理系统</h1>
                </div>
                <div class="m-b"></div>
                <h3>
                    欢迎使用 <strong>水立方后台管理系统</strong>
                </h3>
            </div>
        </div>
        <div class="col-sm-5">
            <form id="signupForm">
                <h3 class="text-center">用户登录</h3>
                <p class="m-t-md text-center">欢迎登录水立方后台管理系统</p>
                <input type="text" id="username" name="username" class="form-control uname" autocomplete="off" />
                <input type="password" id="password" name="password" class="form-control pword m-b" autocomplete="off"/>
                <div class="row">
                    <div class="col-xs-6 pull_left">
                        <div class="form-group">
                            <input class="form-control" type="tel" name="verify" id="verify" placeholder="请输入验证码" maxlength="4">
                        </div>
                    </div>
                    <div class="col-xs-6 pull_left">
                        <a href="javascript:void(0);" rel="external nofollow" title="点击更换验证码">
                            <img style="margin-top: 12px;" id="imgVerify" src="" alt="更换验证码" height="36" width="100%" onclick="getVerify(this);">
                        </a>
                    </div>
                </div>
                <input type="tel" name="code" id="code"
                       class="form-control pword m-b" placeholder="请输入六位数字令牌" maxlength="6"/>
                <input type="hidden" id="secrets" name="secrets" value=""/>
                <a id="login" class="btn btn-login btn-block">登录</a>

            </form>
        </div>
    </div>
    <div class="signup-footer">
        <div class="pull-left">&copy; 2019 All Rights Reserved.
        </div>
    </div>
</div>
<script th:inline="javascript"> var ctx = [[@{/}]] ; </script>
<!-- 全局js -->
<script src="/js/jquery.min.js?v=2.1.4" th:src="@{/js/jquery.min.js?v=2.1.4}"></script>
<script src="/js/bootstrap.min.js?v=3.3.6" th:src="@{/js/bootstrap.min.js?v=3.3.6}"></script>

<!-- 自定义js -->
<script src="/js/content.js?v=1.0.0" th:src="@{/js/content.js?v=1.0.0}"></script>

<!-- jQuery Validation plugin javascript-->
<script src="/js/ajax-util.js"></script>
<script src="/js/plugins/validate/jquery.validate.min.js" th:src="@{/js/plugins/validate/jquery.validate.min.js}"></script>
<script src="/js/plugins/validate/messages_zh.min.js" th:src="@{/js/plugins/validate/messages_zh.min.js}"></script>
<script src="/js/plugins/layer/layer.min.js" th:src="@{/js/plugins/layer/layer.min.js}"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#username').val("");
        $('#password').val("");
        $('#code').css("display", "none");
        $('#login').removeAttr('href');
        $("#login").on('click', function () {
            $("#signupForm").submit();
        });

        validateRule();

        $('#username').blur(function(){
            checkGoogle();
        });
        $('#password').blur(function () {
            checkGoogle();
        });
        $('#verify').focus(function () {
            checkGoogle();
        });
        $("body").keydown(keyDownLogon);
        // getVerify($("#imgVerify"));
        $("#imgVerify").click();
    });

    $.validator.setDefaults({
        submitHandler: function () {
            login();
        }
    });

    function checkGoogle() {
        if(($.trim($('#username').val()) !== "") && ($.trim($('#password').val()) !== "")){
            $.ajax({
                type: "GET",
                url: ctx + "getSecret",
                data: $('#signupForm').serialize(),
                success: function (r) {
                    if (r.code == 0) {
                        $('#code').css("display", "block");
                        $('#secrets').val(r.msg);
                        secretChange(true);
                    } else {
                        $('#code').css("display", "none");
                        secretChange(false);
                    }
                }
            });
        }
    }

    function secretChange(flag) {
        if (flag){
            var icon = "<i class='fa fa-times-circle'></i> ";
            $("#code").rules("add", {
                required : true,
                remote : {
                    url : "/google/login", // 后台处理程序
                    type : "post", // 数据发送方式
                    dataType : "json", // 接受数据格式
                    data : { // 要传递的数据
                        code : function() {
                            return $("#code").val();
                        },
                        secret : function () {
                            return $("#secrets").val();
                        }
                    }
                },
                messages : {
                    required : icon + "请输入您的数字令牌",
                    remote: icon + "数字令牌不正确"
                }
            });
        } else {
            $("#secrets").val("");
            $("#code").rules("remove");
            $("#code").removeClass("error");
            if($("#code-error")){
                $("#code-error").remove();
            }
        }
    }

    function login() {
        $.ajax({
            type: "POST",
            url: ctx + "login",
            data: $('#signupForm').serialize(),
            success: function (r) {
                if (r.code == 0) {
                    var index = layer.load(1, {
                        shade: [0.1, '#fff'] //0.1透明度的白色背景
                    });
                    parent.location.href = '/index?secret=' + $('#secrets').val();
                } else {
                    getVerify($("#imgVerify"));
                    layer.msg(r.msg);
                }
            }
        });
    }

    function keyDownLogon() {
        if (event.keyCode == "13") {
            return false;
        }
    }


    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#signupForm").validate({
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                }
            },
            messages: {
                username: {
                    required: icon + "请输入您的用户名",
                },
                password: {
                    required: icon + "请输入您的密码",
                }
            }
        })
    }

    // function validateRule2() {
    //     var icon = "<i class='fa fa-times-circle'></i> ";
    //     $("#signupForm").validate({
    //         rules: {
    //             username: {
    //                 required: true
    //             },
    //             password: {
    //                 required: true
    //             },
    //             code: {
    //                 required: true,
    //                 remote : {
    //                     url : "/google/login", // 后台处理程序
    //                     type : "post", // 数据发送方式
    //                     dataType : "json", // 接受数据格式
    //                     data : { // 要传递的数据
    //                         code : function() {
    //                             return $("#code").val();
    //                         },
    //                         secret : function () {
    //                             return $("#secrets").val();
    //                         }
    //                     }
    //                 }
    //             }
    //         },
    //         messages: {
    //             username: {
    //                 required: icon + "请输入您的用户名",
    //             },
    //             password: {
    //                 required: icon + "请输入您的密码",
    //             },
    //             code: {
    //                 required: icon + "请输入您的密钥",
    //                 remote: icon + "数字令牌不正确"
    //             }
    //         }
    //     })
    // }

    //获取验证码
    function getVerify(obj) {
        obj.src = "/getVerify?" + Math.random();
        // console.log(obj)
    }
</script>
</body>
</html>
